package org.dyk.mallmanagementsystem.controller;

import org.dyk.mallmanagementsystem.model.Admin;
import org.dyk.mallmanagementsystem.service.admin.AdminService;
import org.dyk.mallmanagementsystem.tools.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private AdminService adminService;

    /**
     * 登录页面
     * @return
     */
    @RequestMapping(value = {"/login"})
    public String login(){
        return "login";
    }

    /**
     * 管理员登录验证
     * 返回boolean值要加@ResponseBody
     * @param adminLoginName
     * @param adminPassword
     * @return
     */
    @RequestMapping(value = {"/checkLogin"}, method = RequestMethod.POST)
    @ResponseBody
    public boolean checkLogin(@RequestParam String adminLoginName,
                              @RequestParam String adminPassword,
                              HttpSession session){
        Admin admin = adminService.login(adminLoginName, adminPassword);
        if (null != admin){
            session.setAttribute(Constants.ADMIN_SESSION,admin);
            return true;
        }else {
            return false;
        }
    }
}
